package com.fita.wechat.entity.search;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

import java.util.Map;

/**
 * @description:
 * @author: 11200
 * @date: 2019/7/9
 */
public class SecondaryAgentOrderSearch implements QueryBuilder {

    private QueryOrder queryOrder;

    private QueryFilter queryFilter;

    private Long secondaryAgentId;


    /**
     * 绑定二级代理ID
     * @param secondaryAgentId
     * @return
     */
    public SecondaryAgentOrderSearch bindSecondaryAgentId(Long secondaryAgentId) {

        this.secondaryAgentId = secondaryAgentId;

        return this;
    }


    /**
     * get prepared filter
     *
     * @param refresh whether to re-create filter
     * @return
     */
    public QueryFilter getPreparedFilter(boolean refresh) {

        if (refresh || queryFilter == null){

            queryFilter = new QueryFilter();

            queryFilter.addEquals("agent.id", secondaryAgentId);
        }
        return queryFilter;
    }

    /**
     * build query string template (sql or hql with :keys)
     *
     * @return
     */
    public String buildSentenceTemplate() {
        return "FROM Order WHERE disabled = false AND status != 'CREATED' " + getPreparedFilter(false).clauseSentence() + getOrder().sentence();
    }

    /**
     * get order
     *
     * @return
     */
    public QueryOrder getOrder() {
        if (queryOrder == null){
            queryOrder = new QueryOrder();
            queryOrder.add("id", "DESC");
        }
        return queryOrder;
    }

    /**
     * build query string template corresponding parameters
     *
     * @return
     */
    public Map<String, Object> buildParameters() {
        return queryFilter.parameters();
    }

    public Long getSecondaryAgentId() {
        return secondaryAgentId;
    }

    public void setSecondaryAgentId(Long secondaryAgentId) {
        this.secondaryAgentId = secondaryAgentId;
    }
}
